//
//  HEMyActiveViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/5/8.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEMyActiveViewController.h"
#import "HEActiveCell.h"
#import "HEGoodsDetailsViewController.h"
#import "HEGhostButtonView.h"
#import "HEActiveModel.h"
#import "HEActiveDetailsViewController.h"
#import "HEMyReportViewController.h"

@interface HEMyActiveViewController ()<UITableViewDelegate, UITableViewDataSource>


@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
 
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HEActiveModel *> * dataSourceArr;
@property (nonatomic, strong) HEGhostButtonView *tabbarView;
@property (nonatomic, strong) NSMutableArray *sections;

@end

@implementation HEMyActiveViewController

- (NSMutableArray<HEActiveModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的活动";
    
    _pageNum = 1;
    _pageSize = 20;
    
    [self initUI];
    [self loadData];
    
    [self setCustomTableView:_tableView];
}

- (void)initUI {
  
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight - ZXSafeAreaBottom) style:UITableViewStyleGrouped];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.separatorColor = KHexColor(@"EEEEEE");

    _tableView.scrollsToTop = YES;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];

    self.tableView.backgroundColor = CViewBgColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

    __weak __typeof(&*self)weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        weakSelf.isFromHeadRefresh = YES;
        weakSelf.pageNum = 1;
        [weakSelf loadData];
    }];

    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        weakSelf.isFromHeadRefresh = NO;
        weakSelf.pageNum += 1;
        [weakSelf loadData];
    }];

    [_tableView registerClass:[HEActiveCell class] forCellReuseIdentifier: KHEActiveCell];
}

#pragma mark -
- (void)initCell {
    // 总段
    _sections = [NSMutableArray array];

    NSMutableArray *classArr = [NSMutableArray array];

    // 收货人
    GDFiterCellModel *nameModel = [GDFiterCellModel new];
    nameModel.cellKlass = HEActiveCell.class;
 
 
    // 添加的时候注意顺序，暂未对顺序做处理
    classArr = @[nameModel].mutableCopy;
    [self.tableView mc_registerCellByFiterModels:classArr];
     
    
    for (HEActiveModel *model in _dataSourceArr) {
        [_sections addObject: nameModel];
    }

    [_tableView reloadData];
}

#pragma mark-

- (void)loadData {
    
    HEPageRequest *params = [HEPageRequest new];
    params.pageNum = _pageNum;
    params.pageSize = _pageSize;
//    [params addParmas: @"脊客" key:@"active_type"];
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KuserActive parameters:params success:^(id  _Nonnull responseObject) {
 
        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }

        HEPageResponse *response = [HEPageResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArr addObjectsFromArray:[HEActiveModel mj_objectArrayWithKeyValuesArray:response.data.list]];
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.list.count == 0) {
                [Dialog toast:@"没有更多数据了~" delay:2.0];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEActiveCell *cell = [tableView dequeueReusableCellWithIdentifier: KHEActiveCell];
    HEActiveModel *model = _dataSourceArr[indexPath.row];
    model.create_date = model.sign_up_date;
    [cell configCellWithModel: _dataSourceArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KHEActiveCell cacheByIndexPath:indexPath configuration:^(HEActiveCell *cell) {
        [cell configCellWithModel: self.dataSourceArr[indexPath.row]];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEActiveModel *model = _dataSourceArr[indexPath.row];
    HEActiveDetailsViewController *vc = [HEActiveDetailsViewController new];
    vc.activeId = model.id;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}
@end
